var https = require('https'),
    fs = require('fs'),
    proxy = require('request');
var options = {
  key: fs.readFileSync('keys/privatekey.pem'),
  cert: fs.readFileSync('keys/certificate.pem')

};

const server = https.createServer(options, (req, res) => {
   if("/favicon.ico" == req.url) {
      return;
   }
   const ip = req.socket.remoteAddress;
   const port = req.socket.remotePort;
   const url = req.url;
   console.log(`Your IP address is ${ip} and your source port is ${port}.`);
   console.log(`req url = ${url}`);
   proxy(('https://maps.googleapis.com'+url), function (error, response, body) {
       if (!error && response.statusCode == 200) {
           const headers = response.headers;
           for (var key in headers) {
               res.setHeader(key, headers[key]);
           }
           res.end(body);
       }
   });
});
server.on('clientError', (err, socket) => {
  socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
console.log("Https server listening on port 3000.");
server.listen(3000);